import { observable, action } from 'mobx';
import api from '../../apis/app';
import { RouteLayout, Browser } from 'weaver-mobile-page';
import { Tools } from 'weaver-mobile';
const getLabel = Tools.getLabel;
export default class RemindPersonStore {
  @observable appid = '';
  @observable hasInit = true;
  @observable fillLoading = false;
  @observable title = '';
  @observable content = getLabel('523162','点击打开表单进行填写') ;
  @observable remindPersonIds = [];
  @observable remindPerson = '';
  @observable remindPersonLoading = false;
  @observable allPersonIds = [];
  @observable unSubmitPerson = [];
  @observable filterAllPerson = [];
  @observable selectRemindPerson = [];
  @observable layout = new RouteLayout.Store({ route: '/form/remind/list', id: 'layout' });
  @observable browser = new Browser.Store({ route: '/form/remind/list/layout', id: 'browser' });

  @action init = appid => {
    this.appid = appid;
    this.hasInit = false;

    api.info(appid).then(res => {
      this.title = `${getLabel('527681','请填写收集表单：')}${res.name}`;
      this.hasInit = true;
    });
  }

  @action onTitleChange = value => {
    this.title = value;
  }

  @action onContentChange = value => {
    this.content = value;
  }

  @action getAllRemindPerson = () => {
    this.remindPersonLoading = true;

    api.statisticsMembers({ appid: this.appid }).then(res => {
      this.remindPersonLoading = false;
      const allPersonIds = [];
      const unSubmitPerson = [];

      res && res.forEach(person => {
        if (!person.submitted) {
          unSubmitPerson.push(person.id);
        }
        allPersonIds.push(person.id);
      });


      this.allPersonIds = allPersonIds;
      this.unSubmitPerson = unSubmitPerson;
      this.filterAllPerson = res;
    });
  }

  @action filterRemindPerson = (name = '', deptId = '', status = '') => {

    api.statisticsMembers({ appid: this.appid, name, deptId }).then(res => {
      if (status === '1' && res) {
        res = res.filter(person => person.submitted === true);
      } else if (status === '2' && res) {
        res = res.filter(person => person.submitted === false);
      }

      this.filterAllPerson = res;
    });
  } 

  onPersonSelect = ids => {
    this.selectRemindPerson = ids;
  }

  @action onRemindPersonChange = key => {
    switch(key) {
      case 'clear':
        this.remindPerson = '';
        this.remindPersonIds = [];
        this.selectRemindPerson = [];
        break;
      case 'unSubmit':
        this.remindPersonIds = this.unSubmitPerson;
        this.remindPerson = getLabel('527678','已选择未填写人员，共$remindPersonSize$人').replace("$remindPersonSize$",this.remindPersonIds.length);
        break;
      case 'all':

        this.remindPersonIds = this.allPersonIds;
        this.remindPerson = getLabel('527679','已选择全部人员，共$remindPersonSize$人').replace("$remindPersonSize$",this.remindPersonIds.length);
        break;
      case 'sure':
        this.remindPersonIds = this.selectRemindPerson;
        this.remindPerson = this.remindPersonIds.length ? getLabel('527680','已选择指定人员，共$remindPersonSize$人').replace("$remindPersonSize$",this.remindPersonIds.length)  : '';
        break;
      }
  }

  @action onCancel = () => {
    this.title = '';
    this.content = getLabel('523162','点击打开表单进行填写') ;
    this.remindPersonIds = [];
    this.remindPerson = '';
  }

  @action fillinRemind = data => {
    this.fillLoading = true;

    return api.fillinRemind(data).then(res => {
      this.fillLoading = false;
    });
  }
}